package com.young.order.controller;

import com.young.order.entity.Order;
import com.young.order.mapper.OrderMapper;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RequestMapping("order")
@RestController
public class OrderController {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RestTemplate restTemplate;


    @RequestMapping("create")
    @GlobalTransactional(name = "create-order",rollbackFor = Exception.class)
    public void insertOrder(@RequestBody Order order){
        orderMapper.insert(order);
        restTemplate.getForObject("http://storage-service/storage"+"/reduce/"+order.getGoodId(),Object.class);
        restTemplate.postForEntity("http://logistics-service/logistics"+"/create/by/order",order,Object.class);
    }
}
